package com.xiaoyu.binaryTree;

import java.util.LinkedList;

/**
 * @program: DS_and_A
 * @description: TODO
 * @author: YuWenYi
 * @create: 2021-05-15 09:53
 **/
/*
        错误思路!
        int depth = 0;
        int d = 0;
        LinkedList<Integer> stack = new LinkedList<>();
        public void inorder(TreeNode root){
        if (root == null) {
            if (!stack.isEmpty())
                d = stack.pop();
            else
                d = 0;
            depth = Math.max(d, depth);
            return;
        }
        d++;
        if (root.left == null && root.right==null){
            stack.push(d);
        }
        inorder(root.left);
        inorder(root.right);
    }*/
public class MaxDepth_104 {


    public int maxDepth(TreeNode root) {
     /*   if (root == null){
            return 0;
        }else {
            int leftDepth = maxDepth(root.left);
            int rightDepth = maxDepth(root.right);
            return Math.max(leftDepth,rightDepth) + 1;  //每次向上回归,高度都会加一
        }*/

        //再简化:
        if (root != null)
            return Math.max(root.left == null ? 0 : maxDepth(root.left),
                    root.right == null ? 0 : maxDepth(root.right)) + 1;
        return 0;
    }




    public static void main(String[] args) {
        MaxDepth_104 depth104 = new MaxDepth_104();
        TreeNode node2 = new TreeNode(3);
        TreeNode node1 = new TreeNode(2,node2,new TreeNode(4));
        TreeNode left = new TreeNode(5);
        TreeNode root = new TreeNode(1,left,node1);
        System.out.println(depth104.maxDepth(root));
    }
}
